package org.cvcsc.weproject.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import org.cvcsc.weproject.controller.dto.RequestDtoInsertClazz;
import org.cvcsc.weproject.controller.dto.ResponseDtoGetAllClazzes;
import org.cvcsc.weproject.controller.dto.ResponseDtoGetClazzById;
import org.cvcsc.weproject.controller.dto.ResponseDtoSaveClazzes;
import org.cvcsc.weproject.domain.Clazz;
import org.cvcsc.weproject.repository.Finder;
import org.cvcsc.weproject.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api/clazz")
public class ClazzController {
        @Autowired
        private ClazzService clazzService;
        @Autowired
        private Finder<Clazz, Long> clazzRepository;

        @PostMapping
        @Operation(summary = "保存班级信息", description = "批量保存班级信息")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功保存班级信息", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ResponseDtoSaveClazzes.class)))),
                        @ApiResponse(responseCode = "400", description = "请求参数有误，无法保存班级信息")
        })
        public List<ResponseDtoSaveClazzes> saveClazzes(
                        @Parameter(description = "班级信息", required = true) @RequestBody List<RequestDtoInsertClazz> clazzes) {
                List<Long> ids = this.clazzService.saveClazzes(clazzes);
                return this.clazzRepository.findAllById(ids).stream()
                                .map(ResponseDtoSaveClazzes::fromEntity)
                                .collect(Collectors.toList());
        }

        @DeleteMapping
        @Operation(summary = "删除所有班级信息", description = "删除系统中所有班级信息")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功删除所有班级信息"),
                        @ApiResponse(responseCode = "500", description = "服务器内部错误，删除失败")
        })
        public void removeAllClazzes() {
                clazzService.removeAllClazzes();
        }

        @GetMapping("/{id}")
        @Operation(summary = "获取单个班级信息", description = "根据班级ID获取班级信息")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功获取班级信息", content = @Content(schema = @Schema(implementation = ResponseDtoGetClazzById.class))),
                        @ApiResponse(responseCode = "404", description = "未找到对应班级信息")
        })
        public ResponseDtoGetClazzById getClazzById(
                        @Parameter(description = "班级ID", required = true) @PathVariable Long id) {
                Clazz clazz = clazzRepository.findById(id)
                                .orElseThrow(() -> new RuntimeException("班级不存在"));
                return ResponseDtoGetClazzById.fromEntity(clazz); // 使用 DTO 的静态工厂方法进行转换
        }

        @GetMapping
        @Operation(summary = "获取所有班级信息", description = "获取所有班级信息列表")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功获取班级信息列表", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ResponseDtoGetAllClazzes.class)))),
                        @ApiResponse(responseCode = "500", description = "服务器内部错误，无法获取班级信息")
        })
        public List<ResponseDtoGetAllClazzes> getAllClazzes() {
                return this.clazzRepository.findAll().stream()
                                .map(ResponseDtoGetAllClazzes::fromEntity) // 使用 DTO 的静态工厂方法进行转换
                                .collect(Collectors.toList());
        }
}